const Sequlize = require('sequelize');
const sequelize = require('../utils/sequlize');
const { Op } = Sequlize;


// 存储用户账户密码模型
const UserModel = sequelize.define('users', {
    id: {
        type: Sequlize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    username: Sequlize.STRING,
    password: Sequlize.STRING,
    name: Sequlize.STRING,
    age: Sequlize.INTEGER,
    gender: {
        type: Sequlize.ENUM('0', '1', '2', '3', '4','5'),
        default: 0
    }
});

sequelize.sync({ force: false })

UserModel.findById = async (id) => {
    return UserModel.findOne({
        where: {
            id
        }
    })
}

UserModel.findByName = async (username) => {
    return UserModel.findOne({
        where: {
            username: username
        }
    })
}

UserModel.insertUser = async (obj) => {
    try{
        await UserModel.create(obj);
        return true;
    }catch (e) {
        console.error(e);
        return false;
    }
}

UserModel.updateUser = async (newObj, option) => {
    try{
        await UserModel.update(newObj, {
        where: option
    })
    } catch(err) {
        console.error('Error:' + err);
    }

}

module.exports = {
    UserModel
}